<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="java.util.Map"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>

<stripes:form name="queryDetailForm" 
	id="queryDetailForm" beanclass="com.feib.soeasy.action.Soez5009ActionBean"	method="post" onsubmit="javascript: return false;">	
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
	<c:if test = "${'1'!=actionBean.detailType}" >
		<c:set var="lastPageNumber" value="${actionBean.queryResult.totalPageCount}"/>
		<c:set var="currentPageNumber" value="${actionBean.queryResult.pageNumber}"/>
		<c:set var="prePageNumber" value="${actionBean.queryResult.pageNumber - 1}"/>
		<c:set var="nextPageNumber" value="${actionBean.queryResult.pageNumber + 1}"/>
		
		<div id="paginationBar">
			<input type="button" id="firstPageBtn-<c:out value='${actionBean.detailType}'/>" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn-<c:out value='${actionBean.detailType}'/>" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText-<c:out value='${actionBean.detailType}'/>" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn-<c:out value='${actionBean.detailType}'/>" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn-<c:out value='${actionBean.detailType}'/>" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
	</c:if>
	<div>
		<table id="listTable">
			<thead>						
				<tr>
					<th>傳輸日期</th>
					<th>超商</th> 	
					<th>CODE</th>
					<th>繳費期限</th>
					<th>繳費日期</th>
					<th>帳單業者</th>	
					<th>繳費帳號</th>				
					<th>繳費金額</th>
					<th>代收手續費</th>
					<th>實撥金額</th>	    
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
				
				<tr>
					<td><c:out value="${fn:substring(viewObj.fbdate,0,4)}/${fn:substring(viewObj.fbdate,4,6)}/${fn:substring(viewObj.fbdate,6,8)}"/></td>
					<td><c:out value="${viewObj.codeName}"/></td>
					<td><c:out value="${viewObj.commissionCode}"/></td>
					<!--  fmt:parseNumber var="dueyear" type="number" pattern="#,###" value="${fn:substring(viewObj.dueday,0,2)}"/ -->
					<c:choose>
						<c:when test="${viewObj.dueday==null}">
							<td align="center"><c:out value="N/A"/></td>
						</c:when>
						<c:otherwise>
							<td><fmt:formatDate value="${viewObj.dueday}" pattern="yyyy/MM/dd" /></td>
						</c:otherwise>
					</c:choose>
					<td><c:out value="${fn:substring(viewObj.aday,0,4)}/${fn:substring(viewObj.aday,4,6)}/${fn:substring(viewObj.aday,6,8)}"/></td>
					<td><c:out value="${viewObj.gpname}"/></td>
					<td><c:out value="${viewObj.userno}"/></td>
					<td align="right"><fmt:formatNumber value="${viewObj.txamt}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.fee}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.txamt-viewObj.fee}" pattern="##,###" /></td>		
				</tr>
				<c:if test = "${rowstat.last}" >
				<tr>
				 	<c:if test="${lastPageNumber<=currentPageNumber ? 'true': 'false'}">
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td align="center">總計</td>
						<td align="right"><c:out value="${viewObj.totalCount}"/>筆</td>
						<td align="right"><fmt:formatNumber value="${viewObj.totalAmount}" pattern="##,###" /></td>
						<td align="right"><fmt:formatNumber value="${viewObj.totalFee}" pattern="##,###" /></td>
						<td align="right"><fmt:formatNumber value="${viewObj.totalAmount-viewObj.totalFee}" pattern="##,###" /></td> 				 
				 	</c:if>		
				</tr>
				</c:if> 
				</c:forEach>
			
				
			</tbody>
		</table>
		</div>
		<stripes:hidden id="hidden5.queryChannel" name="queryChannel"/>
		<stripes:hidden id="hidden5.queryCommissionCode" name="queryCommissionCode"/>	
		<stripes:hidden id="hidden5.qryDateFrom" name="qryDateFrom"/>
		<stripes:hidden id="hidden5.qryDateTo" name="qryDateTo"/>
		<stripes:hidden id="hidden5.detailType" name="detailType"/>
				
		<div id="paginationBar">
			<input type="button" id="doDownloadDetailExcelFileBtn" name="doDownloadExcelFileBtn"  value="下載(Excel)"/>
			<input type="button" id="printBtn2" value="列印" onclick="javascript:divPrint('queryCommissionCodeResultDiv');"/>
		</div>
		
		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog({  title:'<%=UserUtil.getCurrentMenuItemName() %>' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  
			});

			$("[id^='doDownloadDetailExcelFileBtn']").click(function() {
				var queryChannel = $('#hidden5\\.queryChannel').val();
				var detailType = $('#hidden5\\.detailType').val();
				var qryDateFrom = $('#hidden5\\.qryDateFrom').val();
				var qryDateTo = $('#hidden5\\.qryDateTo').val();			
				if('1'==detailType){
					var queryCommissionCode = $('#hidden5\\.queryCommissionCode').val();
					doDownloadDetailExcelFileBtn(queryChannel,queryCommissionCode,qryDateFrom,qryDateTo  );
				}else if('2'==detailType){
					var queryCommissionCode = '';
					doDownloadDetailExcelFileBtn(queryChannel,queryCommissionCode,qryDateFrom,qryDateTo  );
				}else if('3'==detailType){
					doDownloadTotalExcelFileAction();
				}
				
			});			
		});
		//-->
		</script>	
	
	<c:if test = "${'1'!=actionBean.detailType}" >
	<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$("[id^='firstPageBtn-']").click(function() {
				var uid = this.id.substring(13);
				if(uid == 3)
					doQueryTotalAction(1);
				else if (uid == 2)
					doQueryChannelActionPage(1);					
			});
			
			$("[id^='prePageBtn-']").click(function() {
				var uid = this.id.substring(11);
				if(uid == 3)
					doQueryTotalAction(<c:out value='${prePageNumber}'/>);
				else if (uid == 2)
					doQueryChannelActionPage(<c:out value='${prePageNumber}'/>);					
		
			});
			
			$("[id^='nextPageBtn-']").click(function() {
				var uid = this.id.substring(12);
				if(uid == 3)
					doQueryTotalAction(<c:out value='${nextPageNumber}'/>);
				else if (uid == 2)
					doQueryChannelActionPage(<c:out value='${nextPageNumber}'/>);	
			});
			
			$("[id^='lastPageBtn-']").click(function() {
				var uid = this.id.substring(12);
				if(uid == 3)
					doQueryTotalAction(<c:out value='${lastPageNumber}'/>);
				else if (uid == 2)
					doQueryChannelActionPage(<c:out value='${lastPageNumber}'/>);
			});
			
			$("[id^='currentPageText-']").change(function() {
				var uid = this.id.substring(16);
				var value = this.value;
				if (value != ''){
					value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
					var intRegex = /^\d+$/;
					if(!intRegex.test(value)) {
						alert('頁數必須是整數');
						return ;
					}
					
					value = parseInt(value, 10);
					
					if (value < 1){
						alert('頁數必須是大於1');
						return ;
					}
					else if (value > <c:out value='${lastPageNumber}'/>){
						alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
						return ;
					}
					else if (value != <c:out value='${currentPageNumber}'/>){
						if(uid == 3)							
							doQueryTotalAction(value);
						else if (uid == 2)
							doQueryChannelActionPage(value);
					}
					
				}
				else{
					alert('頁數不可為空白');
					return ;
				}
			});
	
			
		});
		//-->
		</script>	
	</c:if>	
	</c:when>
	<c:otherwise>
	
	
		
	<table id="listTable">
			<thead>						
				<tr>
					<th>傳輸日期</th>
					<th>超商</th> 	
					<th>CODE</th>
					<th>繳費期限</th>
					<th>繳費日期</th>
					<th>帳單業者</th>	
					<th>繳費帳號</th>				
					<th>繳費金額</th>
					<th>代收手續費</th>
					<th>實撥金額</th>	    
				</tr>
			</thead>
		</table>	
		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog({  title:'<%=UserUtil.getCurrentMenuItemName() %>' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  
			});

		
		});
		//-->
		</script>			
	</c:otherwise>
</c:choose>
</stripes:form>